"""
scipy_analyze.py

Created by Bud Gibson on 2010-08-11.
Copyright (c) Bud Gibson 2010. Released under the Artistic/GPL License.

This script is meant to be run in an interactive pylab session activated by ipython -pylab
"""

import config
from reporter import get_sample
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import scipy.stats as ss

# Get data sets
# Pull full data set out to friend of a friend
participants = get_sample(2)
#generate an array of symmetric tie counts for people showing ties publicly
pb_sym_ties = np.array([p.count_ties for p in participants if p.public==True])
#generate an array of in network symmetric ties counts for those showing ties publicly
pb_net_sym_ties = np.array([p.count_net_ties for p in participants if p.public==True])
#generate an array of in network symmetric ties counts for those not showing ties publicly
pr_net_sym_ties = np.array([p.count_net_ties for p in participants if p.public==False])
#generate an array of participants who generated an exception
participants_exceptions = [p for p in participants if p.public=='exception']

#generate some stats
pb_sym_percentiles = [ss.scoreatpercentile(pb_sym_ties, pctl) for pctl in
                     [25, 50, 75, 90, 95, 100]]
                     
pb_net_sym_percentiles = [ss.scoreatpercentile(pb_net_sym_ties, pctl) for pctl in
                         [25, 50, 75, 90, 95, 100]]

pr_net_sym_percentiles = [ss.scoreatpercentile(pr_net_sym_ties, pctl) for pctl in
                        [25, 50, 75, 90, 95, 100]]


#now plot the histogram of all ties
n, bins, patches = plt.hist(pb_sym_ties, 1000, color='green')
plt.ylabel('Buzz Participant Count')
plt.xlabel('Number of Two-Way Ties')
plt.title('Two-Way Tie Frequency in a Buzz Network of 10,110')
plt.axis([1, 450, 0, 600])
plt.axvline(x=50, color='black', linestyle='--', linewidth=1.0)
plt.axvline(x=394, color='black', linestyle='--', linewidth=1.0)
plt.axvline(x=159.24, color='red', alpha=0.50, linewidth=1.0)
plt.text(394,350,'90th percentile = 394', rotation='vertical', ha='right', va='bottom')
plt.text(50,350,'median = 50 ties', rotation='vertical', ha='right', va='bottom')
plt.text(159,350,'mean = 159', rotation='vertical', ha='right', va='bottom')
plt.savefig('two-way-tie-frequency.png')

#now plot the histogram of in-network public ties
plt.figure(2)
plt.hist(pb_net_sym_ties, 1000, color='green')
plt.axis([1,75,0,1200])
plt.ylabel('Buzz Participant Count')
plt.xlabel('Number of Ties')
plt.title('\"Public\" In-network Tie Frequency (n = 7225)')
plt.axvline(x=16, color='black', linestyle='--', linewidth=1.0)
plt.axvline(x=58, color='black', linestyle='--', linewidth=1.0)
plt.axvline(x=59.59, color='red', alpha=0.50, linewidth=1.0)
plt.text(58,350,'75th percentile = 58', rotation='vertical', ha='right', va='bottom')
plt.text(16,350,'median = 16 ties', rotation='vertical', ha='right', va='bottom')
plt.text(59.59,350,'mean = 59.6', rotation='vertical', ha='right', va='bottom')
plt.savefig('in-network-public-tie-frequency.png')

#now plot the histogram of in-network private ties
plt.figure(3)
plt.hist(pr_net_sym_ties, 1000, color='blue')
plt.axis([1,75,0,700])
plt.ylabel('Buzz Participant Count')
plt.xlabel('Number of Ties')
plt.title('\"Private\" In-network Tie Frequency (n = 2885)')
plt.axvline(x=11, color='black', linestyle='--', linewidth=1.0)
plt.axvline(x=48, color='black', linestyle='--', linewidth=1.0)
plt.axvline(x=73.2, color='red', alpha=0.50, linewidth=1.0)
plt.text(48,350,'75th percentile = 48', rotation='vertical', ha='right', va='bottom')
plt.text(11,350,'median = 11 ties', rotation='vertical', ha='right', va='bottom')
plt.text(73.2,350,'mean = 73.2', rotation='vertical', ha='right', va='bottom')
plt.savefig('in-network-private-tie-frequency.png')

#plot the scatter of all public ties against in-network public ties
plt.figure(4)
plt.scatter(pb_net_sym_ties,pb_sym_ties,c='green')
plt.axis([-100,2000,-100,5000])
plt.ylabel('Reported Ties')
plt.xlabel('Inferred Ties')
plt.title('Reported vs. Inferred Ties (n = 7225)')

#show the regression line and label it
(ar, br) = np.polyfit(pb_net_sym_ties, pb_sym_ties, 1)
yr = np.polyval([ar, br], pb_net_sym_ties)
plt.plot(pb_net_sym_ties,yr,'r-',lw=2)
plt.text(1900,4260,'Reported ~ 2.5 Inferred',rotation=38, ha='right', size='x-large')

plt.savefig('scatter-total-v-in-network-ties.png')


def main():
  pass


if __name__ == '__main__':
  main()

